package com.lagou.factory;

import com.lagou.annotion.MyAutowired;
import com.lagou.annotion.MyService;
import com.lagou.service.AccountService;
import com.lagou.utils.TransactionManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @Author ZHAO Yudong
 * @Date 2020/11/6 14:42
 * @description：
 */
@Slf4j
@MyService("proxyFactory")
public class ProxyFactory {

    @MyAutowired
    private TransactionManager transactionManager;

    @MyAutowired
    private AccountService accountService;

    public void setTransactionManager(TransactionManager transactionManager) {
        this.transactionManager = transactionManager;
    }

    /**
     * Jdk动态代理
     */
    public Object getJdkProxy(Object obj) {
        // 获取代理对象
        return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(),
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        Object result = null;
                        try {
                            System.out.println("开启事务");//(关闭事务的自动提交)
                            transactionManager.beginTransaction();
                            result = method.invoke(obj, args);//调用具体的业务方法
                            System.out.println("提交事务");
                            transactionManager.commit();
                        } catch (Exception e) {
                            //e.printStackTrace();
                            System.out.println("错误原因 = " + e.getMessage());
                            System.out.println("回滚事务");
                            transactionManager.rollback();
                            throw e;
                        } finally {
                            System.out.println("关闭事务");
                            transactionManager.release();
                        }
                        return result;
                    }
                });
    }

    /**
     * 使用cglib动态代理生成代理对象
     */
    public Object getCglibProxy(Object obj) {
        return Enhancer.create(obj.getClass(), new MethodInterceptor() {
            @Override
            public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
                Object result = null;
                try {
                    // 开启事务(关闭事务的自动提交)
                    transactionManager.beginTransaction();
                    result = method.invoke(obj, objects);
                    // 提交事务
                    transactionManager.commit();
                } catch (Exception e) {
                    e.printStackTrace();
                    // 回滚事务
                    transactionManager.rollback();
                    // 抛出异常便于上层servlet捕获
                    throw e;
                }
                return result;
            }
        });
    }
}
